#!/usr/bin/env perl
# ------------------------------------------------------------------------
#   File Name: gscs
#      Author: Zhao Yanbai
#              Tue Feb 14 13:42:22 2012
# Description:  Generate Shell Code String
#               在对可执行文件执行objdump -d xxx > xxx.S后
#               在xxx.S中找到ShellCode处的代码将之生成成
#               \x55\x89\xe5\x90\x90\x90\x90\xb8\xbb\xaa\x00\x00\x5d\xc3
#               式的客串
# ------------------------------------------------------------------------
use strict;
# Find Shell Code

my $file = @ARGV[0];
my $line = int(@ARGV[1]);

unless($file) {
    print "Error: no input file!\n";
}


open S, "<", $file;

my $find=0;

if($line) {
    my $cl = 0;
    while((++$cl<$line) && <S>) { }
    $find=1 if $cl == $line;
} else {
    while(<S>) {
        if(m#(\d|[a-fA-F])+ <(.+)>:.*#) {
            if(lc($2) eq "shellcode") {
                $find = 1;
                last;
            }
        }
    }
}

exit unless $find;

my $codeline;
while(<S>) {
    if(m#.*:\t(.+\t).*#) {
        $codeline = $1;
        $codeline =~ s/\s+$//;
        my @code = split(" ", $codeline);
        foreach (@code) {
            print "\\x" . $_;
        }
    } else {
        last;
    }
}

print "\n";

close S;
